package com.surfilter.service.impl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.surfilter.bean.Didi;
import com.surfilter.bean.GpsConfig;
import com.surfilter.bean.MapData;
import com.surfilter.bean.Param;
import com.surfilter.bean.Period;
import com.surfilter.bean.QueryResult;
import com.surfilter.bean.QueryTask;
import com.surfilter.bean.VirtualIdentity;
import com.surfilter.changesource.LayuiPageInfo;
import com.surfilter.dao.AnalysisMapper;
import com.surfilter.dao.VirtualMapper;
import com.surfilter.service.PhoneService;
import com.surfilter.util.DateUtil;
import com.surfilter.util.InitUtil;
import com.surfilter.util.ProcessUtil;
import com.surfilter.util.StringUtil;

@Service
public class PhoneServiceImpl implements PhoneService{
	
	/**分析持久层接口*/
	@Autowired
	private AnalysisMapper analysisMapper;
	
	/**虚拟身份持久层接口*/
	@Autowired
	private VirtualMapper virtualMapper;
	
	/**
	 * 处理时间段查询条件（1、时间查看，2、时间段查看）
	 * 
	 * @param  MapData 地图数据
	 * @date   2018-5-13
	 * @author Zhaoqifeng
	 */
	public void processTimeCondition(MapData mapData) throws Exception {
		if (mapData.getTimeType() == 2) {
			String startDay = mapData.getStartTime_day(); //开始日期
			String endDay = mapData.getEndTime_day(); //结束日期
			String startHour = mapData.getStartTime_hours(); //开始时段
			String endHour = mapData.getEndTime_hours(); //结束时段
			if (StringUtils.isNotBlank(startDay) && StringUtils.isNotBlank(endDay) &&
				StringUtils.isNotBlank(startHour) && StringUtils.isNotBlank(endHour)) {
				//初始化查询条件
				List<Param> params = new ArrayList<Param>();
				//计算间隔天数
				long dayMilliseconds = 24 * 60 * 60 * 1000;
				long startDayMilliseconds = DateUtil.string2Date(startDay, DateUtil.DEFAULT_DATE_FORMAT).getTime();
				long endDayMilliseconds = DateUtil.string2Date(endDay, DateUtil.DEFAULT_DATE_FORMAT).getTime();
				int intervalDays = (int) ((endDayMilliseconds - startDayMilliseconds) / dayMilliseconds);
				//生成查询条件，放入参数类
				for (int i = 0; i <= intervalDays; i++) {
					Param param = new Param();
					param.setStartTime(startDay + " " + startHour);
					param.setEndTime(startDay + " " + endHour);
					params.add(param);
					//第二天时间
					startDayMilliseconds += dayMilliseconds;
					startDay = DateUtil.date2String(new Date(startDayMilliseconds), DateUtil.DEFAULT_DATE_FORMAT);
				}
				mapData.setParam(params);
			}
		}
	}
	
	/**
	 * 查询账号的分析结果
	 * 
	 * @param  MapData 地图数据
	 * @return Map<String, Object> 地图数据集合（包括位置集合、各位置停留总时间、各位置停留总数、各位置上网时间段）
	 * @date   2017-7-3
	 * @author Zhaoqifeng
	 */
	@Override
	public List<MapData> queryAccountAnalysis(MapData mapData) throws Exception {
		//结果集合
		List<MapData> result = new ArrayList<MapData>();
		//处理时间段查询条件（1、时间查看，2、时间段查看）
		processTimeCondition(mapData);
		//查询地图坐标信息（访问时间，日志类型，经度，纬度）
		List<MapData> mapDatas = analysisMapper.selectAccountAnalysis(mapData);
		if (null != mapDatas && !mapDatas.isEmpty()) {
			//地图坐标信息集合
			Map<String, MapData> positions = new HashMap<String, MapData>();
			int size = mapDatas.size();
			for (int i = 0; i < size; i++) {
				//初始化停留时间
				long stopTime = 1L;
				//获取地图坐标信息
				MapData frontMapData = mapDatas.get(i);
				String frontAccessTime = frontMapData.getAccess_Time();
				String frontX = frontMapData.getX();
				String frontY = frontMapData.getY();
				String behindAccessTime = null;
				if (i != size - 1) {
					MapData behindMapData = mapDatas.get(i+1);
					behindAccessTime = behindMapData.getAccess_Time();
					String behindX = behindMapData.getX();
					String behindY = behindMapData.getY();
					
					//停留时间（单位：秒）
					stopTime = (DateUtil.string2DateTime(behindAccessTime).getTime() - DateUtil.string2DateTime(frontAccessTime).getTime()) / 1000;
					stopTime = stopTime == 0 ? 1 : stopTime;
					
					//是否失去监测（是否切换基站，是否超时）
					if ((!frontX.equals(behindX) || !frontY.equals(behindY)) && stopTime > 30 * 60) {
						stopTime = 60;
						//最新结束时间
						long time = DateUtil.string2Date(frontAccessTime, DateUtil.DEFAULT_DATETIME_FORMAT).getTime() + stopTime * 1000;
						behindAccessTime = DateUtil.date2String(new Date(time), DateUtil.DEFAULT_DATETIME_FORMAT);
					}
				} else {
					//最后一次访问时间为前面的访问时间
					behindAccessTime = frontAccessTime;
				}
				//每个时间段和其中停留时间
				Period period = new Period(frontAccessTime, behindAccessTime, stopTime);
				if (positions.containsKey(frontX + "," + frontY)) {
					//已存在的坐标信息
					MapData existMapData = positions.get(frontX + "," + frontY);
					String lineTimeType = existMapData.getLine_Time_Type();
					String frontLineTimeType = frontMapData.getLine_Time_Type();
					if (!lineTimeType.contains(frontLineTimeType)) {
						existMapData.setLine_Time_Type(lineTimeType + "," + frontLineTimeType);
					}
					//已存在的时间段详情集合
					List<Period> periods = existMapData.getList();
					periods.add(period);
					existMapData.setList(periods); //设置停留时间段和各时间段停留时间（开始时间、结束时间、停留时间）
					existMapData.setTime(existMapData.getTime() + stopTime); //设置总停留时间
					existMapData.setTotal(existMapData.getTotal() + 1); //设置总停留次数
					positions.put(frontX + "," + frontY, existMapData);
				} else {
					//时间段详情集合
					List<Period> periods = new ArrayList<Period>();
					periods.add(period);
					frontMapData.setList(periods); //设置停留时间段和各时间段停留时间（开始时间、结束时间、停留时间）
					frontMapData.setTime(stopTime); //设置总停留时间
					frontMapData.setTotal(1L); //设置总停留次数
					positions.put(frontX + "," + frontY, frontMapData);
				}
			}
			//地图坐标信息集合排序（按停留总时间递减排序）
			ArrayList<Map.Entry<String, MapData>> list = new ArrayList<Map.Entry<String, MapData>>(positions.entrySet());  
	        Collections.sort(list, new Comparator<Map.Entry<String, MapData>>() {
				@Override
				public int compare(Entry<String, MapData> o1, Entry<String, MapData> o2) {
					return (int) (o2.getValue().getTime() - o1.getValue().getTime());
				}
	        });
	        //设置每个地图坐标的停留详情
	        for (int i = 0; i < list.size(); i++) {
	        	MapData data = list.get(i).getValue();
	        	data.setStartTime(data.getList().get(0).getStartTime());
	        	data.setEndTime(data.getList().get(data.getList().size()-1).getEndTime());
	        	result.add(data);
	        }
		}
		return result;
	}
	
	/**
	 * 查询账号的统计结果
	 * 
	 * @param  MapData 地图数据
	 * @return Map<String, Object> 统计数据集合（虚拟身份集合、24小时上网曲线、访问量排行、中文词组、滴滴位置）
	 * @date   2017-7-3
	 * @author Zhaoqifeng
	 */
	@Override
	public Map<String, Object> queryAccountStatistic(MapData mapData) throws Exception {
		//处理时间段查询条件（1、时间查看，2、时间段查看）
		processTimeCondition(mapData);
		//结果集合
		Map<String, Object> result = new HashMap<String, Object>();
		
		//账号虚拟身份查询
		List<VirtualIdentity> virtualIdentitys = virtualMapper.selectVirtualIdentity(mapData);
		if (virtualIdentitys != null && !virtualIdentitys.isEmpty()) {
			//终端信息集合
			List<VirtualIdentity> terminal = new ArrayList<VirtualIdentity>();
			//虚拟身份集合
			List<VirtualIdentity> virtual = new ArrayList<VirtualIdentity>();
			for (VirtualIdentity virtualIdentity : virtualIdentitys) {
				int type = virtualIdentity.getType();
				if (type == 1) {
					terminal.add(virtualIdentity);
				} else if (type == 2) {
					virtual.add(virtualIdentity);
				}
			}
			//放入结果集合
			result.put("terminal", terminal);
			result.put("virtual", virtual);
		}
		
		//24小时上网日志
		List<MapData> netCurve = analysisMapper.selectAccountInternet(mapData);
		result.put("netCurve", netCurve);
		
		//访问URL日志
		List<Didi> urls = analysisMapper.selectAccountUrl(mapData);
		if (null != urls && !urls.isEmpty()) {
			//访问量排行集合
			Map<String, Long> views = new HashMap<String, Long>();
			//GPS位置集合
			List<Didi> gps = new ArrayList<Didi>();
			//GPS位置去重
			Set<String> removal = new HashSet<String>();
			
			for (Didi didi : urls) {
				String url = didi.getUrl();
				String account = didi.getAccount();
				String accessTime = didi.getAccess_Time();
				
				//访问量排行统计
				String domain = StringUtil.deepUrl(url); //域名
				if (StringUtils.isNotBlank(domain)) {
					//判断域名是否为ip地址，如果不为ip地址，放入集合中
					boolean ip = StringUtil.isIP(domain);
					if (!ip) {
						if (views.containsKey(domain)) {
							Long num = views.get(domain);
							views.put(domain, num + 1);
						} else {
							views.put(domain, 1L);
						}
					}
				}
				
				//提取GPS位置
				Map<String,GpsConfig> map = InitUtil.gpsMap;
				for (Map.Entry<String, GpsConfig> entry : map.entrySet()) {
					GpsConfig iden= entry.getValue();
					String gpsrule = iden.getGpsrule();
					if (StringUtils.isNotBlank(gpsrule)) {
						if (url.indexOf(gpsrule.toLowerCase()) != -1 || url.indexOf(gpsrule.toUpperCase()) != -1) {
							String x = null;
							String y = null;
							String lnglow = check(iden.getLng().toLowerCase() + "([^&]*)", url);
							String lngupp = check(iden.getLng().toUpperCase() + "([^&]*)", url);
							String latlow = check(iden.getLat().toLowerCase() + "([^&]*)", url);
							String latupp = check(iden.getLat().toUpperCase() + "([^&]*)", url);
							if (StringUtils.isNotBlank(lnglow) && StringUtils.isNotBlank(latlow)) {
							    x = lnglow;
							    y = latlow;
							}
							if (StringUtils.isNotBlank(lngupp) && StringUtils.isNotBlank(latupp)) {
								x = lngupp;
								y = latupp;
							}
							if (StringUtils.isNotBlank(x) && StringUtils.isNotBlank(y) && (!x.equals("0.0") || !y.equals("0.0"))) {
								StringBuffer temp = new StringBuffer();
		                    	temp.append(x).append("-").append("y").append("-").append(accessTime);
		                    	//去重
		                    	if (!removal.contains(temp.toString())) {
		                    		removal.add(temp.toString());
		                    		didi.setX(x);
									didi.setY(y);
									didi.setAccount(account);
									didi.setAccess_Time(accessTime);
									didi.setName(iden.getName());
									didi.setPicturepath(iden.getPicturepath());
									gps.add(didi);
		                    	}
							}
						}
					} else {
						String x = null;
						String y = null;
						String lnglow = check(iden.getLng().toLowerCase() + "([^&]*)", url);
						String lngupp = check(iden.getLng().toUpperCase() + "([^&]*)", url);
						String latlow = check(iden.getLat().toLowerCase() + "([^&]*)", url);
						String latupp = check(iden.getLat().toUpperCase() + "([^&]*)", url);
						if (StringUtils.isNotBlank(lnglow) && StringUtils.isNotBlank(latlow)) {
						    x = lnglow;
						    y = latlow;
						}
						if (StringUtils.isNotBlank(lngupp) && StringUtils.isNotBlank(latupp)) {
							x = lngupp;
							y = latupp;
						}
						if (StringUtils.isNotBlank(x) && StringUtils.isNotBlank(y) && (!x.equals("0.0") || !y.equals("0.0"))) {
							StringBuffer temp = new StringBuffer();
	                    	temp.append(x).append("-").append("y").append("-").append(accessTime);
	                    	//去重
	                    	if (!removal.contains(temp.toString())) {
	                    		removal.add(temp.toString());
	                    		didi.setX(x);
								didi.setY(y);
								didi.setAccount(account);
								didi.setAccess_Time(accessTime);
								didi.setName(iden.getName());
								didi.setPicturepath(iden.getPicturepath());
								gps.add(didi);
	                    	}
						}
					}
				}
			}
			result.put("viewNum", ProcessUtil.sortMap(views));
			result.put("gps", gps);
		}
		return result;
	}
	
	@Override
	public PageInfo<QueryTask> queryAccountTask(int pageNum) throws Exception {
		PageHelper.startPage(pageNum, 7);
		List<QueryTask> list = analysisMapper.queryAccountTask();
		PageInfo<QueryTask> pageInfo = new PageInfo<QueryTask>(list);
		return pageInfo;
	}
	
	@Override
	public LayuiPageInfo<QueryResult> queryAccountResult(QueryResult queryResult) throws Exception {
		PageHelper.startPage(queryResult.getPageNum(), queryResult.getLimit());
		List<QueryResult> list = analysisMapper.queryAccountResult(queryResult);
		LayuiPageInfo<QueryResult> pageInfo = new LayuiPageInfo<QueryResult>(list);
		return pageInfo;
	}
	
	private String check(String regex, String url) {
		Pattern p = Pattern.compile(regex);
		Matcher m = p.matcher(url);
		String result = "";
		if(m.find()) {
			result = m.group(1);
		}
		return result;
	}

	@Override
	public Map<String, Object> queryAccountTrack(MapData mapData) throws Exception {
		List<MapData> positions = analysisMapper.selectAccountTrack(mapData);
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("positions", positions); //list集合
		return result;
	}
	
	@Override
	@Transactional
	public Long modifyResultPosition(Long set_Id, List<Map<String,String>> points) throws Exception {
		long total = 0L;
		//查询结果ID
		List<MapData> mapDatas = analysisMapper.selectAccountResultId(set_Id);
		if (mapDatas != null && !mapDatas.isEmpty()) {
			total = mapDatas.size();
			//地图坐标点个数
			int pointSize = points.size();
			long quotient = total / pointSize; //商数
			long remainder = total % pointSize; //余数
			int temp = 0;
			for (Map<String,String> point : points) {
				//将多余数据平均分到前几次更新
				int rownum = (int) (remainder > 0 ? quotient + 1 : quotient);
				remainder --;
				for (int i = temp; i < total; i++) {
					if (rownum == 0) {
						break;
					}
					MapData mapData = mapDatas.get(i);
					mapData.setX(point.get("x")); //经度
					mapData.setY(point.get("y")); //纬度
					mapData.setAddress(point.get("address")); //地址
					rownum -- ;
					temp ++;
				}
			}
			analysisMapper.updateAccountResultPosition(mapDatas);
		}
		return total;
	}

	@Override
	@SuppressWarnings("unchecked")
	public Map<String, Object> queryPositionAnalysis(MapData mapData) throws Exception {
		Map<String, Object> result = new HashMap<String, Object>();
		List<MapData> mapDatas = analysisMapper.selectPositionAnalysis(mapData);
		if(null != mapDatas && !mapDatas.isEmpty()) {
			//得到热点数据、轨迹数据、基站分组数据（每2小时一段，0点-2点...）
			Map<String, Object> map = getHotspotTrajectoryAndHourly(mapDatas);
			List<String> hotspotMapDatas = (List<String>)map.get("hotspotMapDatas");
			//List<MapData> trajectoryMapDatas = (List<MapData>)map.get("trajectoryMapDatas");
			List<Map<String, MapData>> hourlyMapDatas = (List<Map<String, MapData>>)map.get("hourlyMapDatas");
			//得到基站分组数据（每12小时一段，6点-18点，0点-6点&&18点-24点）
			List<Map<String, MapData>> dayNightMapDatas = getDayNight(mapDatas);
			//得到前5活跃位置（每2小时一段，0点-2点...）
			List<Map<String, MapData>> topFivePositions = getTopFivePositions(hourlyMapDatas);
			//得到工作地（6点-18点）、居住地（0点-6点&&18点-24点）位置
			List<Map<String, MapData>> dayNightPositions = getDayNightPositions(dayNightMapDatas);
			result.put("hotspot", hotspotMapDatas); //热点分布
			//result.put("trajectory", trajectoryMapDatas); //轨迹
			result.put("topFive", topFivePositions); //前5活跃位置
			result.put("newPosition", mapDatas.get(mapDatas.size()-1)); //最新位置
			result.put("dayPosition", dayNightPositions.get(0)); //白天位置
			result.put("nightPosition", dayNightPositions.get(1)); //黑天位置
		}
		return result;
	}
	
	public Map<String, Object> getHotspotTrajectoryAndHourly(List<MapData> mapDatas) {
		List<Map<String, MapData>> hourlyMapDatas = initHourlyMapDatas();
		Map<String, Object> result = new HashMap<String, Object>();
		List<String> hotspotMapDatas = new ArrayList<String>();
		//List<MapData> trajectoryMapDatas = new ArrayList<MapData>();
		Set<String> times = new LinkedHashSet<String>();
		int remark = 0;
		int size = mapDatas.size();
		for(int i=0; i<size; i++) {
			MapData frontMapData = mapDatas.get(i);
			getHotspotMapDatas(frontMapData, hotspotMapDatas);
			//getTrajectoryMapDatas(frontMapData, trajectoryMapDatas);
			String accessTime = frontMapData.getAccess_Time();
			times.add(accessTime);
			if(i != size-1) {
				MapData backMapData = mapDatas.get(i+1);
				String frontX = frontMapData.getX();
				String frontY = frontMapData.getY();
				String frontHour = frontMapData.getHour();
				String backX = backMapData.getX();
				String backY = backMapData.getY();
				String backHour = backMapData.getHour();
				if(!(frontX.equals(backX) && frontY.equals(backY)) || classifyByHourly(frontHour)!=classifyByHourly(backHour)) {
					MapData mapData = mapDatas.get(remark).clone();
					long startTime = Timestamp.valueOf(mapData.getAccess_Time()).getTime();
					long endTime = Timestamp.valueOf(accessTime).getTime();
					long time = (endTime-startTime) / 1000;
					time = time != 0 ? time : 1;
					mapData.setTime(time);
					mapData.setTimes(times);
					getTimeSliceMapDatas(mapData, hourlyMapDatas, 0);
					times.clear();
					remark = i+1;
				}
			}else {
				MapData mapData = mapDatas.get(remark).clone();
				long startTime = Timestamp.valueOf(mapData.getAccess_Time()).getTime();
				long endTime = Timestamp.valueOf(mapDatas.get(size-1).getAccess_Time()).getTime();
				long time = (endTime-startTime) / 1000;
				time = time != 0 ? time : 1;
				mapData.setTime(time);
				mapData.setTimes(times);
				getTimeSliceMapDatas(mapData, hourlyMapDatas, 0);
			}
		}
		result.put("hotspotMapDatas", hotspotMapDatas);
		//result.put("trajectoryMapDatas", trajectoryMapDatas);
		result.put("hourlyMapDatas", hourlyMapDatas);
		return result;
	}
	
	public List<Map<String, MapData>> getDayNight(List<MapData> mapDatas) {
		List<Map<String, MapData>> dayNightMapDatas = initDayNightMapDatas();
		Set<String> times = new LinkedHashSet<String>();
		int remark = 0;
		int size = mapDatas.size();
		for(int i=0; i<size; i++) {
			MapData frontMapData = mapDatas.get(i);
			String accessTime = frontMapData.getAccess_Time();
			times.add(accessTime);
			if(i != size-1) {
				MapData backMapData = mapDatas.get(i+1);
				String frontX = frontMapData.getX();
				String frontY = frontMapData.getY();
				String frontHour = frontMapData.getHour();
				String backX = backMapData.getX();
				String backY = backMapData.getY();
				String backHour = backMapData.getHour();
				if(!(frontX.equals(backX) && frontY.equals(backY)) || classifyByDayNight(frontHour)!=classifyByDayNight(backHour)) {
					MapData mapData = mapDatas.get(remark);
					long startTime = Timestamp.valueOf(mapData.getAccess_Time()).getTime();
					long endTime = Timestamp.valueOf(accessTime).getTime();
					long time = (endTime-startTime) / 1000;
					time = time != 0 ? time : 1;
					mapData.setTime(time);
					mapData.setTimes(times);
					getTimeSliceMapDatas(mapData, dayNightMapDatas, 1);
					times.clear();
					remark = i+1;
				}
			}else {
				MapData mapData = mapDatas.get(remark);
				long startTime = Timestamp.valueOf(mapData.getAccess_Time()).getTime();
				long endTime = Timestamp.valueOf(mapDatas.get(size-1).getAccess_Time()).getTime();
				long time = (endTime-startTime) / 1000;
				time = time != 0 ? time : 1;
				mapData.setTime(time);
				mapData.setTimes(times);
				getTimeSliceMapDatas(mapData, dayNightMapDatas, 1);
			}
		}
		return dayNightMapDatas;
	}
	
	public List<Map<String, MapData>> getTopFivePositions(List<Map<String, MapData>> hourlyMapDatas) {
		for(Map<String, MapData> map : hourlyMapDatas) {
			ArrayList<Map.Entry<String, MapData>> list = new ArrayList<Map.Entry<String, MapData>>(map.entrySet());
			Collections.sort(list, new Comparator<Map.Entry<String, MapData>>() {
				@Override
				public int compare(Entry<String, MapData> o1, Entry<String, MapData> o2) {
					return (int)(o2.getValue().getTime() - o1.getValue().getTime());
				}
			});
			map.clear();
			int size = list.size();
			for(int i=0; i<5&&i<size; i++) {
				MapData mapData = list.get(i).getValue();
				String x = mapData.getX();
				String y = mapData.getY();
				int isFind = getPositionIsFind(x, y);
				mapData.setIsFind(isFind);
				map.put(list.get(i).getKey(), mapData);
			}
		}
		return hourlyMapDatas;
	}
	
	public List<Map<String, MapData>> getDayNightPositions(List<Map<String, MapData>> dayNightMapDatas) {
		for(Map<String, MapData> map : dayNightMapDatas) {
			ArrayList<Map.Entry<String, MapData>> list = new ArrayList<Map.Entry<String, MapData>>(map.entrySet());
			Collections.sort(list, new Comparator<Map.Entry<String, MapData>>() {
				@Override
				public int compare(Entry<String, MapData> o1, Entry<String, MapData> o2) {
					return (int)(o2.getValue().getTime() - o1.getValue().getTime());
				}
			});
			map.clear();
			int size = list.size();
			for(int i=0; i<size; i++){
				if(list.get(0).getValue().getTime()-list.get(i).getValue().getTime()<200){
					MapData mapData = list.get(i).getValue();
					map.put(list.get(i).getKey(), mapData);
				}else{
					break;
				}
			}
		}
		return dayNightMapDatas;
	}

	public List<Map<String, MapData>> initHourlyMapDatas() {
		List<Map<String, MapData>> hourlyMapDatas = new ArrayList<Map<String, MapData>>();
		Map<String, MapData> map1 = new LinkedHashMap<String, MapData>();
		Map<String, MapData> map2 = new LinkedHashMap<String, MapData>();
		Map<String, MapData> map3 = new LinkedHashMap<String, MapData>();
		Map<String, MapData> map4 = new LinkedHashMap<String, MapData>();
		Map<String, MapData> map5 = new LinkedHashMap<String, MapData>();
		Map<String, MapData> map6 = new LinkedHashMap<String, MapData>();
		Map<String, MapData> map7 = new LinkedHashMap<String, MapData>();
		Map<String, MapData> map8 = new LinkedHashMap<String, MapData>();
		Map<String, MapData> map9 = new LinkedHashMap<String, MapData>();
		Map<String, MapData> map10 = new LinkedHashMap<String, MapData>();
		Map<String, MapData> map11 = new LinkedHashMap<String, MapData>();
		Map<String, MapData> map12 = new LinkedHashMap<String, MapData>();
		hourlyMapDatas.add(map1);
		hourlyMapDatas.add(map2);
		hourlyMapDatas.add(map3);
		hourlyMapDatas.add(map4);
		hourlyMapDatas.add(map5);
		hourlyMapDatas.add(map6);
		hourlyMapDatas.add(map7);
		hourlyMapDatas.add(map8);
		hourlyMapDatas.add(map9);
		hourlyMapDatas.add(map10);
		hourlyMapDatas.add(map11);
		hourlyMapDatas.add(map12);
		return hourlyMapDatas;
	}
	
	public List<Map<String, MapData>> initDayNightMapDatas() {
		List<Map<String, MapData>> dayNightMapDatas = new ArrayList<Map<String, MapData>>();
		Map<String, MapData> map1 = new LinkedHashMap<String, MapData>();
		Map<String, MapData> map2 = new LinkedHashMap<String, MapData>();
		dayNightMapDatas.add(map1);
		dayNightMapDatas.add(map2);
		return dayNightMapDatas;
	}
	
	public void getHotspotMapDatas(MapData mapData, List<String> hotspotMapDatas) {
		String x = mapData.getX();
		String y = mapData.getY();
		int isFind = getPositionIsFind(x, y);
		if(0 == isFind) {
			hotspotMapDatas.add(x + "," + y);
		}
	}
	
	public void getTrajectoryMapDatas(MapData mapData, List<MapData> trajectoryMapDatas) {
		String lineTimeType = mapData.getLine_Time_Type();
		if(StringUtils.isBlank(lineTimeType)) {
			mapData.setLine_Time_Type("1"); //上线
			trajectoryMapDatas.add(mapData);
		}else if("1".equals(lineTimeType) || "2".equals(lineTimeType)) {
			trajectoryMapDatas.add(mapData);
		}
	}
	
	public void getTimeSliceMapDatas(MapData mapData, List<Map<String, MapData>> timeSliceMapDatas, int type) {
		int temp = 0;
		String x = mapData.getX();
		String y = mapData.getY();
		String key = x + "," + y;
		String hour = mapData.getHour();
		if(0 == type) {
			temp = classifyByHourly(hour);
		}else if(1 == type) {
			temp = classifyByDayNight(hour);
		}
		Map<String, MapData> map = timeSliceMapDatas.get(temp);
		if(map.containsKey(key)) {
			MapData oldMapData = map.get(key);
			long time = oldMapData.getTime() + mapData.getTime();
			oldMapData.setTime(time);
			oldMapData.getTimes().addAll(mapData.getTimes());
			map.put(key, oldMapData);
		}else {
			Set<String> times = new LinkedHashSet<String>();
			times.addAll(mapData.getTimes());
			mapData.setTimes(times);
			map.put(key, mapData);
		}
		timeSliceMapDatas.set(temp, map);
	}
	
	public int classifyByHourly(String hour) {
		int temp = 0;
		if("00".equals(hour) || "01".equals(hour)) {
			temp = 0;
		}else if("02".equals(hour) || "03".equals(hour)) {
			temp = 1;
		}else if("04".equals(hour) || "05".equals(hour)) {
			temp = 2;
		}else if("06".equals(hour) || "07".equals(hour)) {
			temp = 3;
		}else if("08".equals(hour) || "09".equals(hour)) {
			temp = 4;
		}else if("10".equals(hour) || "11".equals(hour)) {
			temp = 5;
		}else if("12".equals(hour) || "13".equals(hour)) {
			temp = 6;
		}else if("14".equals(hour) || "15".equals(hour)) {
			temp = 7;
		}else if("16".equals(hour) || "17".equals(hour)) {
			temp = 8;
		}else if("18".equals(hour) || "19".equals(hour)) {
			temp = 9;
		}else if("20".equals(hour) || "21".equals(hour)) {
			temp = 10;
		}else if("22".equals(hour) || "23".equals(hour)) {
			temp = 11;
		}
		return temp;
	}
	
	public int classifyByDayNight(String hour) {
		int temp = 0;
		if("06".equals(hour)||"07".equals(hour) || "08".equals(hour)|| "09".equals(hour)|| "10".equals(hour)|| "11".equals(hour)
				|| "12".equals(hour)|| "13".equals(hour)|| "14".equals(hour)|| "15".equals(hour)|| "16".equals(hour)
				|| "17".equals(hour)) {
			temp = 0;
		}else if("00".equals(hour)||"01".equals(hour) || "02".equals(hour)|| "03".equals(hour)|| "04".equals(hour)|| "05".equals(hour)
				|| "18".equals(hour)|| "19".equals(hour)|| "20".equals(hour)|| "21".equals(hour)|| "22".equals(hour)
				|| "23".equals(hour)) {
			temp = 1;
		}
		return temp;
	}
	
	public int getPositionIsFind(String x, String y) {
		return StringUtils.isNotBlank(x) && StringUtils.isNotBlank(y) && (Double.parseDouble(x) > Double.parseDouble(y)) ? 0 : 1;
	}
}